using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace Common
{
    public class XmlManager
    {
        string sMode, sIP, sServerName, sDatabaseName, sUser, sPass, strConnect, sTimeOut;
        public void WriteDefaultConnectionInfo(String ServerCom, String DBName, String DBUser, String SDPass)
        {
            XmlTextWriter writer = new XmlTextWriter( CommonHeader.CONNECT_INFO_FILE , System.Text.Encoding.UTF8);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;
            writer.WriteStartDocument();
            writer.WriteStartElement("Root");
            writer.WriteString("");
            writer.WriteStartElement("Server");
            writer.WriteString(ServerCom);
            writer.WriteEndElement();
            writer.WriteStartElement("DBName");
            writer.WriteString(DBName);
            writer.WriteEndElement();
            writer.WriteStartElement("User");
            writer.WriteString(DBUser);
            writer.WriteEndElement();
            writer.WriteStartElement("DBPass");
            writer.WriteString(SDPass);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();
            writer.Close();
        }

        public string readXml()
        {
            try
            {
                XmlTextReader reader = new XmlTextReader(CommonHeader.CONNECT_INFO_FILE);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.LocalName.Equals("Mode"))
                        {
                            sMode = reader.ReadString();
                        }
                        if (reader.LocalName.Equals("IP"))
                        {
                            sIP = reader.ReadString();
                        }
                        if (reader.LocalName.Equals("Server"))
                        {
                            sServerName = reader.ReadString();
                        }
                        if (reader.LocalName.Equals("DBName"))
                        {
                            sDatabaseName = reader.ReadString();
                        }
                        if (reader.LocalName.Equals("User"))
                        {
                            sUser = reader.ReadString();
                        }
                        if (reader.LocalName.Equals("DBPass"))
                        {
                            sPass = reader.ReadString();
                        }
                        if (reader.LocalName.Equals("TimeOut"))
                        {
                            sTimeOut = reader.ReadString();
                        }
                    }                   
                }
                reader.Close();
                if (sMode == "0")
                {
                    strConnect = "Data Source= " + sServerName + " ;Initial Catalog= "
                            + sDatabaseName + " ;Persist Security Info=True;User ID="
                            + sUser + " ; Password = " + sPass + "; Connection Timeout = "+ sTimeOut;
                }
                else if (sMode == "1")
                {
                    //strConnect = "Network Library=DBMSSOCN; Data Source= " + sIP + " ;Initial Catalog= "
                    //        + sDatabaseName + " ;Persist Security Info=True;User ID="
                    //        + sUser + " ; Password = " + sPass + "";
                    strConnect = "Data Source= " + sIP + ";Network Library=DBMSSOCN;Initial Catalog= " + sDatabaseName + "; User ID=" + sUser + " ; Password = " + sPass + "; Connection Timeout = " + sTimeOut;
                }
                //SqlConnection conn = new SqlConnection(strConnect);
                //conn.Open();
                return strConnect;
            }
            catch (SqlException ex)
            {
                return CommonHeader.NO_CONNECTION;                    
            }                
        }
    }
}
